﻿using System.Collections.Generic;
using System.Windows.Media;

namespace Red.Toolkit.Profiler.Logic
{
    /// <summary>
    /// Single event block in the profiler trace
    /// </summary>
    public abstract class Event
    {
        public class Comparer : IComparer<Event>
        {
            public int Compare( Event x, Event y )
            {
                // assuming there is not events starting at the same moment (there shouldn't be)
                return x.Evt.StartTime.CompareTo( y.Evt.StartTime );
            }
        }

        public enum DrawStyle
        {
            Normal,
            Selected,
            Highlighted
        }

        public static DrawStyle CombineDrawStyles( DrawStyle styleA, DrawStyle styleB )
        {
            if ( styleA == DrawStyle.Highlighted || styleB == DrawStyle.Highlighted )
                return DrawStyle.Highlighted;
            if ( styleA == DrawStyle.Selected || styleB == DrawStyle.Selected )
                return DrawStyle.Selected;
            return DrawStyle.Normal;
        }

        // TODO: reduce number of fields, it takes quite a lot of space
        public ProfilerDataView ParentView;
        public RawEvent Evt;
        public Color Color; // TODO: what about brush?
        public int Level;
        public DrawStyle Style;

        public List<Event> DependencyStart;
        public List<Event> DependencyEnd;

        public Event( ProfilerDataView parentView, RawEvent org, Color color )
        {
            Evt = org;
            Color = color;
            ParentView = parentView;
            Level = 0;
            DependencyStart = null;
            DependencyEnd = null;
        }

        public double GetLength()
        {
            return Evt.EndTime - Evt.StartTime;
        }

        public abstract float GetWeight();
        public abstract string GetCaption();

        public string Name { get { return GetCaption(); } }
        public string FormattedLength { get { return Utils.FormatValue( GetLength(), ValueType.Time ); } }

        public void AddDependencyStart(Event e)
        {
            if ( DependencyStart == null )
                DependencyStart = new List<Event>();
            DependencyStart.Add( e );
        }

        public void AddDependencyEnd( Event e )
        {
            if ( DependencyEnd == null )
                DependencyEnd = new List<Event>();
            DependencyEnd.Add( e );
        }
    }
}
